/**
 * 申诉处理业务共通js
 * 
 *  功能点：
 *      1.申诉同意
 *      2.申诉驳回
 *      3.主动关闭异常
 *   
 *   create by:zhengxb 2019.9.22 
 *   
 * */
var MY_APPEAL = (function() {
	
	/**
	 * 异常处理模态框
	 * 
	 * @param illegalId     错误ID
	 * @param originBtnName 主页面按下的按钮
	 * @param callback      回调函数
	 * 
	 * */
	function createModalWin( illegalId,originBtnName,callback,callback2){
		
		var e = initAppealModalHtml();
		
		var $modal = $(e).appendTo('body').modal('show');
		
		$modal.one('show.bs.modal', function() {
			// 无处理
		}).one('shown.bs.modal', function() {
			
//			$modal.draggable();//设置为可移动
			
			//清空处理意见
			$('#feedback').val('');
			$('#feedback').focus();
			
			// 调用源按钮名字check
			if ( originBtnName !='check' && 
				 originBtnName !='closeIllegal'){
				
				WeKnow.errorMsg('页面的按钮id不符合规范');
				return;
			}
			// 根据不同场景显示不同按钮
			if ( originBtnName == 'check' ){
				$('#reject').show();
				$('#agree').show();
				$('#closeIllegal').hide();
			} else {
				$('#reject').hide();
				$('#agree').hide();
				$('#closeIllegal').show();
			}
			
			// 同意、驳回、关闭异常  按钮按下
			$modal.find('.fun-save-but').on('click',function(){
				var $that = $(this);
				
				
				var feedbackSt = 0;
				var targetBtn = $that.prop('id');
				
				if ( targetBtn == 'reject'){
					feedbackSt = -1;
				} else if (targetBtn == 'agree') {
					feedbackSt = 0;
				}else if (targetBtn == 'closeIllegal') {
					feedbackSt = 10;
				}
				
				var a = {};//DtInvsIllegalAppealEntity
				
				a.illegalId = illegalId;
				// 处理意见
				var feedback =$.trim( $('#feedback').val());
				if ( feedback.length )  {
					a.feedback = feedback;
				}
				a.feedbackSt = feedbackSt;//
				
				WeKnow.loading();
				
				$.ajax({ 
			        type: "POST", 
			        url: "invs_appeal.do/update", 
			        dataType: "json",
			        contentType:"application/json",
			        data: JSON.stringify(a),
			        async: true,//异步调用
			        success: function(data) {
			        	
			        WeKnow.closeLoading();
			        	
					if (!WeKnow.filterAjaxResult(data)) return false; // session过期check
			        	
			        	if (data.errcode == 0) {
			        		
			        		WeKnow.successMsg("更新成功");
			        		
			        		$modal.modal('hide');
			        		
			        		if ('function' === typeof callback) callback();//回调
			        		
			        		if ('function' === typeof callback2) callback2();//回调2
			        		
			        	} else if (data.errcode == -1 ) {
			        		WeKnow.errorMsg( data.errMsg );
			        	} else {
			        		WeKnow.systemMsg('查询失败，请重新登录后再试');
			        	}
			        }
				});
			});
		}).one('hidden.bs.modal', function() {
			$modal.remove(); // 取消事件注册
		});
	}
		
	/**
	 * 生成申诉状态模态框
	 * */ 
	function initAppealModalHtml(){
		var e = '';
		
		e+='<div class="modal fade" tabindex="-1" role="dialog" id="appeal_dispose">';
		  e+='<div class="modal-dialog" role="document">';
		    e+='<div class="modal-content">';
		      e+='<div class="modal-header">';
		        e+='<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
		        e+='<h4 id="" class="modal-title">申诉处理</h4>';
		      e+='</div>';
		      e+='<div class="modal-body">';
		        e+='<div class="row" style="padding-left:0px;">';
		        	e+='<div class="col-xs-12">';
	           			e+='<label>请输入处理意见：</label>';
	        		e+='</div>';
	           		e+='<div class="col-xs-12">';
	           			e+='<textarea class="form-control" id="feedback" style="ime-mode:inactive" rows="3"></textarea>';
	        		e+='</div>';
	        		e+='<div class="col-xs-12 text-right">';
	           			e+='<button type="button" id="reject" class="btn btn-primary fun-save-but"  style="width:90px;margin: 5px;display:none;">驳回申诉</button>';
	           			e+='<button type="button" id="agree" class="btn btn-primary fun-save-but"  style="width:90px;margin: 5px;display:none;">同意</button>';
	           			e+='<button type="button" id="closeIllegal" class="btn btn-primary fun-save-but"  style="width:90px;margin: 5px;display:none;">关闭异常</button>';
	        		e+='</div>';
	        	e+='</div>';
		      e+='</div>';
		    e+='</div>';
		  e+='</div>';
		e+='</div>';
		
		return e;
	}
			
	/**
	 * 申诉状态文本公开方法
	 * */ 
	function getAppealStTxt(appealSt){
		
		var retVal = '';
		switch(appealSt) {
			case 0:
				retVal = '未申诉';
		        break;
			case 1:
				retVal = '申诉中';
		        break;
			case -1:
				retVal = '被驳回';
		        break;
			case 2:
				retVal = '申诉成功';
		        break;
			case 10:
				retVal = '手动关闭';
		        break;
		     default:
		} 
		return retVal;
	}
	
	// add by zhengxb 2019.6.21  end ---------------------------------------------------------
	return {
		createModalWin:createModalWin,
		getAppealStTxt:getAppealStTxt
	}
})();